// 
//  Copyright 2011  Joel Johnson
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using System.Linq;
using System.Reflection;
using Servlets.Attributes;
using System.Collections.Generic;
namespace Api {
	public static class ReflectionUtils {
		/// <summary>
		/// Gets a method marked with TagAttribute that matches the given name.
		/// </summary>
		public static MethodInfo GetMethodWithTagAttribute(this Type type, String name) {
			MethodInfo methodWithMethodName = null;
			foreach(MethodInfo method in type.GetMethods()) {
				object[] attributes = method.GetCustomAttributes(typeof(TagAttribute), false);
				if(attributes.Length > 0) {
					if((attributes[0] as TagAttribute).Name == name) {
						return method;
					}
				}
				if(method.Name == name) {
					//if there was a method with the name given, return it.
					//but only if there are no methods with a Tag.Name value == name
					methodWithMethodName = method;
				}
			}
			return methodWithMethodName; 
		}
	}
}

